using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CampaignWP7.Base;
using cocos2d;
using CocosDenshion;
using Microsoft.Xna.Framework;

namespace CampaignWP7
{
    class EnemyBullet : DynamicsObj
    {
        private static int collidePointNum = 2;
        protected CollideCircle[] m_collisionPoint;

        public static int m_iCurPlayFireEffect = -1;
        public static int m_iCurPlayBalloonBombEffect = -1;
        //
        public BulletState CurrentState { get; set; }
        public enum BulletState
        {
            BS_ALIVE = 0,
            BS_DYING,
            //
            BS_NUM
        }
        //
        enum AnimationType
        {
            AT_NORMAL = 0,
            AT_EXPLODE,
            //
            AT_NUM
        }
        private static string[] ActionsName = {"normal","explode"};
        private static int[] ActionsFrameCount = { 4, 10 };
        protected static Dictionary<string, List<CCSpriteFrame>> m_framesDict;

        //
        //
        public EnemyBullet()
        {
            string bulletTextureName = "bullet";
            //damage value
            Damage = 1;
            Power = 1;
            CurrentState = BulletState.BS_ALIVE;
            //collide point
            m_collisionPoint = new CollideCircle[collidePointNum];
            for (int i = 0; i < collidePointNum;++i )
            {
                //TODO
                m_collisionPoint[i] = new CollideCircle(new Vector2(44 - i * 10, 0), 3);
            }
            //this is a dict of frame
            //key:Animation name  
            //value:List of SpriteFrame
            if (m_framesDict == null || m_framesDict.Count == 0)
            {
                m_framesDict = new Dictionary<string, List<CCSpriteFrame>>();
                for (int i = 0; i < (int)AnimationType.AT_NUM; ++i)
                {
                    List<CCSpriteFrame> temp = new List<CCSpriteFrame>();
                    //---
                    //every animation type shouldn't greater then N!
                    //---
                    for (int k = 0; k < ActionsFrameCount[i]; ++k)
                    {
                        string frameName = ActionsName[i];
                        //the format of name likes  "bullet_normal_1.png"
                        frameName = bulletTextureName + "_" + frameName + "_" + k + ".png";
                        CCSpriteFrame spriteframe = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(frameName);
                        if (spriteframe != null)
                        {
                            temp.Add(spriteframe);
                        }
                        //
                        if (k == ActionsFrameCount[i] - 1)
                        {
                            if (temp.Count != 0)
                            {
                                m_framesDict.Add(ActionsName[i], temp);
                            }
                            break;
                        }
                    }
                }
            }

            //
            //this is the main sprite to draw,so we should add in the batch 
            string initName = bulletTextureName + "_" + ActionsName[0] + "_0.png";
            this.initWithSpriteFrameName(initName);
            anchorPoint = new CCPoint(0.5f, 0.5f);
            //

        }
        public override void draw()
        {
            base.draw();
        }
        //
        public override void update(float dt)
        {
            for (int i = 0; i < collidePointNum; ++i)
            {
                //TODO
                CCPoint nodeP = new CCPoint(0,0);
                nodeP = convertToWorldSpaceAR(new CCPoint(44 - i * 10, 0));
                m_collisionPoint[i].center = new Vector2(nodeP.x, nodeP.y);
            }
            base.update(dt);
        }
        public void PlayShootAnimation()
        {
            CurrentState = BulletState.BS_ALIVE;
            //
            List<CCSpriteFrame> temp = m_framesDict[ActionsName[(int)AnimationType.AT_NORMAL]];
            CCAnimation ani = CCAnimation.animationWithFrames(temp);
            CCAnimate action = CCAnimate.actionWithDuration(.3f, ani, true);
            this.runAction(CCRepeatForever.actionWithAction(action));

            //sound
            if (m_iCurPlayFireEffect != -1)
            {
                SimpleAudioEngine.sharedEngine().stopEffect((uint)m_iCurPlayFireEffect);
                m_iCurPlayFireEffect = -1;
            }
            m_iCurPlayFireEffect = (int)SimpleAudioEngine.sharedEngine().playEffect(ResDef.g_BunnyFire);
        }
        //
        public void PlayExplodeAnimationThenRemove(CCPoint pos)
        {
            CurrentState = BulletState.BS_DYING;
            //
            this.Spin(true, new CCPoint(pos.x, pos.y));
            List<CCSpriteFrame> temp = m_framesDict[ActionsName[(int)AnimationType.AT_EXPLODE]];
            CCAnimation ani = CCAnimation.animationWithFrames(temp);
            CCAnimate action = CCAnimate.actionWithDuration(1.0f, ani, true);
            CCFiniteTimeAction call = CCCallFuncND.actionWithTarget(this, callbackRemove, this);
            CCAction explode = CCSequence.actionOneTwo(action, call);
            this.stopAllActions();
            this.runAction(explode);

            //sound
            if (m_iCurPlayBalloonBombEffect != -1)
            {
                SimpleAudioEngine.sharedEngine().stopEffect((uint)m_iCurPlayBalloonBombEffect);
                m_iCurPlayBalloonBombEffect = -1;
            }
            m_iCurPlayBalloonBombEffect = (int)SimpleAudioEngine.sharedEngine().playEffect(ResDef.g_BalloonBomb);
        }
        public void callbackRemove(CCNode sender, object data)
        {
            Bullet bullet = (Bullet)data;
            DynamicMgr.Instance().DelChild(bullet);

        }
        //
        public void StopAnimation()
        {
            this.stopAllActions();
        }
        public int Damage{ get;set; }
        public int Power{ get; set;}


        public CollideCircle[] CollidePt
        {
            get { return m_collisionPoint; }
        }
    }
}
